package com.kakavr.emp.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.beust.jcommander.internal.Maps;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.split.service.ISplitLogService;
import com.kakavr.util.SessionUtils;

/**
 * 店铺分账
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("shopSplit")
public class ShopSplitController extends BaseController {
	@Resource
	private ISplitLogService splitLogService;

	/**
	 * 分账列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectList", method = RequestMethod.GET)
	public Map<String, Object> selectSplitList(HttpServletRequest request) {
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("status", request.getParameter("status"));
		map.put("storeId", SessionUtils.getShopId());
		return MsgResult
				.okResultData(splitLogService.selectSplitLogByPage(map));
	}

	/**
	 * 分账详情
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectInfo", method = RequestMethod.GET)
	public Map<String, Object> selectSplitInfo(HttpServletRequest request) {
		String splitId = request.getParameter("splitId");
		if (StringUtils.isBlank(splitId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "分账id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("splitId", splitId);
		return MsgResult.okResultData(splitLogService.selectSplitInfo(map));
	}

	/**
	 * 审核分账
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "examSplit", method = RequestMethod.POST)
	public Map<String, Object> examSplit(HttpServletRequest request) {
		String splitId = request.getParameter("splitId");
		if (StringUtils.isBlank(splitId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "分账id");
		}
		String examStatus = request.getParameter("examStatus");
		if (StringUtils.isBlank(examStatus)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "审核状态");
		}
		String remark = request.getParameter("remark");
		if ("2".equals(examStatus)) {
			if (StringUtils.isBlank(remark)) {
				throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
						MsgConstants.MSG_CONTENT_1001, "审核理由");
			}
		} else {
			examStatus = "1";
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", splitId);
		map.put("examStatus", examStatus);
		map.put("remark", remark);
		splitLogService.updateSplitLogByExam(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 提交凭证
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "voucherSplit", method = RequestMethod.POST)
	public Map<String, Object> voucherSplit(HttpServletRequest request){
		String splitId = request.getParameter("splitId");
		if (StringUtils.isBlank(splitId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "分账id");
		}
		String wxImg = request.getParameter("wxImg");
		String aliImg = request.getParameter("aliImg");
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", splitId);
		map.put("wxImg", wxImg);
		map.put("aliImg", aliImg);
		map.put("imgStatus", "0");
		splitLogService.updateSplitLogByExam(map);
		return MsgResult.okResult();
	}

}
